680. 验证回文串 II

680. 验证回文串 II

Similar Question

leading to the advanced question

Solution Tips

方案一: 对撞指针 + 类似递归

function validPalindrome(s) {
	let low = 0, high = s.length - 1;
	while (low < high) {
		const c1 = s[low], c2 = s[high];
		if (c1 == c2) {
			++low;
			--high;
		} else {
			return isValidPalindrome(s, low, high - 1) || isValidPalindrome(s, low + 1, high);
		}
	}
	return true;
}

function isValidPalindrome(s, low, high) {
	for (let i = low, j = high; i < j; ++i, --j) {
		const c1 = s[i], c2 = s[j];
		if (c1 != c2) {
			return false;
		}
	}
	return true;
}